package itcast.bos.base.service.imp;

import java.util.Date;
import java.util.Set;
import java.util.UUID;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import itcast.bos.base.dao.OrderRespotory;
import itcast.bos.base.service.IAreaService;
import itcast.bos.base.service.IFixedAreaService;
import itcast.bos.base.service.IWorkBillService;
import itcast.bos.base.service.OrderService;
import itcast.bos.domain.base.Area;
import itcast.bos.domain.base.Courier;
import itcast.bos.domain.base.FixedArea;
import itcast.bos.domain.base.SubArea;
import itcast.bos.domain.finaldemo.FinalDemo;
import itcast.bos.domain.orders.Order;
import itcast.bos.domain.orders.WorkBill;

@Service
@Transactional
public class OrderServiceImp implements OrderService {

	@Autowired
	private OrderRespotory or;
	@Autowired
	private IFixedAreaService ias;
	@Autowired
	private IAreaService as;

	// 保存订单
	@Override
	public void addOrder(Order order) {
		System.out.println("保存订单。。。。。。。。。。。。。。。。。。。。。。。");
		String sendAddress = order.getSendAddress();
		order.setOrderNum(UUID.randomUUID().toString());
		Area sendA = order.getSendArea();
		// System.out.println("sendA================================" + sendA);
		Area preSend = as.findByProvinceAndCityAndDistrict(sendA.getProvince(), sendA.getCity(), sendA.getDistrict());
		// System.out.println("preSend--------------------------------" +
		// preSend);
		Area recA = order.getRecArea();
		Area preRec = as.findByProvinceAndCityAndDistrict(recA.getProvince(), recA.getCity(), recA.getDistrict());
		order.setSendArea(preSend);
		order.setRecArea(preRec);
		// 1根据详细地址可以找到的
		String faid = WebClient.create(FinalDemo.Crm_Manager_Url
				+ "/crm_management/services/customerService/findFixedId?address=" + sendAddress).get(String.class);
		if (faid != null) {
			// 找到定去了
			// 根据定去ID找到定区 和对应的快递员
			FixedArea fa = ias.findOne(faid);

			// 关联快递员
			Courier c = fa.getCouriers().iterator().next();
			if (c != null) {
				// order.setCourier(c);
				// 设置订单号

				bindCuorier(order, c);
				System.out.println("关联客户成功。。。。。。");
				//发短信 生成工单
				toWorkBill(order);
				return;
			}

		}
		// 没有找到定区的 通过省市区 去查找分区 确定定区 取派员

		Set<SubArea> subs = preSend.getSubareas();
		for (SubArea subArea : subs) {
			if (order.getSendAddress().contains(subArea.getKeyWords())
					|| order.getSendAddress().contains(subArea.getAssistKeyWords())) {
				Courier c = subArea.getFixedArea().getCouriers().iterator().next();
				if (c != null) {
					bindCuorier(order, c);
					toWorkBill(order);
					return;
				}
			}
		}
		// 3 转到人工调度
		order.setOrderType("2");
		or.save(order);
		//toWorkBill(order);
		return;

	}
	@Autowired
	private IWorkBillService ibs;
	@Autowired
	private JmsTemplate jt;
	//发短信  生成工单
	public void  toWorkBill(final Order order) {
		WorkBill wb=new WorkBill();
		wb.setBuildtime(new Date());
		wb.setCourier(order.getCourier());
		wb.setOrder(order);
		wb.setType("新");
		wb.setPickstate("新单");
		wb.setRemark(order.getRemark());
		final String smsNum = RandomStringUtils.randomNumeric(4);
		wb.setSmsNumber(smsNum);
		ibs.save(wb);
		jt.send("bos_sms",new MessageCreator(){

			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage mapMess = session.createMapMessage();
				mapMess.setString("telephone", order.getTelephone());
				mapMess.setString("msg", "短信序号：" + smsNum + ",取件地址：" + order.getSendAddress()
				+ ",联系人:" + order.getSendName() + ",手机:"
				+ order.getSendMobile() + "，快递员捎话："
				+ order.getSendMobileMsg());
				return mapMess;
			}
			
		});
		wb.setPickstate("已通知。。。");
		System.out.println("生成了工单。。。。");
		
		
		
	}
	
	
	//自动分单
	public void bindCuorier(Order order, Courier c) {
		order.setCourier(c);
		// order.setOrderNum(UUID.randomUUID().toString());
		order.setOrderType("1");
		or.save(order);
		System.out.println("自动分担成功。。。。");
	}

}
